package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.domain.Dictionary;
import cn.wolfcode.crm.mapper.DictionaryItemMapper;
import cn.wolfcode.crm.mapper.DictionaryMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IDictionaryService;
import cn.wolfcode.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DictionaryServiceImpl implements IDictionaryService {
    @Autowired
    private DictionaryMapper dictionaryMapper;
    @Autowired
    private DictionaryItemMapper dictionaryItemMapper;

    @Override
    public void deleteByPrimaryKey(Long id) {
        dictionaryItemMapper.deleteByParentId(id);
        dictionaryMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdate(Dictionary entity) {
        if (entity.getId() == null) {
            dictionaryMapper.insert(entity);
        } else {
            dictionaryMapper.updateByPrimaryKey(entity);
        }
    }


    @Override
    public Dictionary selectByPrimaryKey(Long id) {
        return dictionaryMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Dictionary> selectAll() {
        return dictionaryMapper.selectAll();
    }


    @Override
    public PageResult query(QueryObject qo) {
        Integer total = dictionaryMapper.query4Count(qo);
        if (total == 0) {
            return PageResult.PAGERESULT_EMPTY;
        }
        return new PageResult(total, dictionaryMapper.query4List(qo));
    }
}
